﻿using Dapper;
using System;
using BCEntity.Common.Solution.WebsiteSolutionItem;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.Common.Solution.WebsiteSolutionItem
{
    /// <summary>
    /// 官网解决方案项目
    /// </summary>
    public class WebsiteSolutionItemData : IWebsiteSolutionItemData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM websitesolutionitem";
        private const string QUERY = @"SELECT WebsiteSolutionItemId,CreateAdminId,CreateAdminName,CreateTime,UpdateTime,EditAdminId,EditAdminName,SolutionItemName,WebsiteSolutionId,SolutionName,Icon,Background,LinkUrl,Content,Enabled FROM websitesolutionitem";
        public WebsiteSolutionItemData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #region 检查官网解决方案项目是否存在
        public async Task<bool> Exists(int websiteSolutionItemId)
        {
            string sql = string.Format(@"{0} WHERE WebsiteSolutionItemId=?WebsiteSolutionItemId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteSolutionItemId", websiteSolutionItemId, DbType.Int32);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 添加官网解决方案项目
        public async Task<WebsiteSolutionItemEntity> Add(WebsiteSolutionItemEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("websitesolutionitem", new string[] { "CreateAdminId", "CreateAdminName", "UpdateTime", "SolutionItemName", "WebsiteSolutionId", "SolutionName", "Content", "Enabled" });
            sql = string.Format("{0} {1} WHERE WebsiteSolutionItemId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
            parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?UpdateTime", entity.UpdateTime, DbType.DateTime);
            parameters.Add("?SolutionItemName", entity.SolutionItemName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?WebsiteSolutionId", entity.WebsiteSolutionId, DbType.Int32, size: 11);
            parameters.Add("?SolutionName", entity.SolutionName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?Content", entity.Content, DbType.String);
            parameters.Add("?Enabled", entity.Enabled, DbType.Boolean);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WebsiteSolutionItemEntity>(sql, parameters, databaseContext.Transaction);

        }
        #endregion

        #region 修改官网解决方案项目
        public async Task<WebsiteSolutionItemEntity> Update(WebsiteSolutionItemEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("websitesolutionitem", new string[] { "CreateAdminId", "CreateAdminName", "UpdateTime", "EditAdminId", "EditAdminName", "SolutionItemName", "WebsiteSolutionId", "SolutionName", "Icon", "Background", "LinkUrl", "Content", "Enabled" }, new string[] { "WebsiteSolutionItemId" });
            sql = string.Format("{0} {1} WHERE WebsiteSolutionItemId=?WebsiteSolutionItemId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteSolutionItemId", entity.WebsiteSolutionItemId, DbType.Int32, size: 11);
            parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
            parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?UpdateTime", entity.UpdateTime, DbType.DateTime);
            parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
            parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?SolutionItemName", entity.SolutionItemName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?WebsiteSolutionId", entity.WebsiteSolutionId, DbType.Int32, size: 11);
            parameters.Add("?SolutionName", entity.SolutionName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?Icon", entity.Icon?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?Background", entity.Background?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?LinkUrl", entity.LinkUrl?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?Content", entity.Content, DbType.String);
            parameters.Add("?Enabled", entity.Enabled, DbType.Boolean);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WebsiteSolutionItemEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 删除官网解决方案项目
        public async Task<bool> Delete(int websiteSolutionItemId)
        {
            string sql = @"DELETE FROM websitesolutionitem WHERE WebsiteSolutionItemId=?WebsiteSolutionItemId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteSolutionItemId", websiteSolutionItemId, DbType.Int32);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 获取官网解决方案项目
        public async Task<WebsiteSolutionItemEntity> GetEntity(int websiteSolutionItemId)
        {
            string sql = string.Format(@"{0} WHERE WebsiteSolutionItemId=?WebsiteSolutionItemId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteSolutionItemId", websiteSolutionItemId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WebsiteSolutionItemEntity>(sql, parameters);
        }
        #endregion

        #region 获取官网解决方案项目列表
        public async Task<IEnumerable<WebsiteSolutionItemEntity>> GetList(int websiteSolutionId)
        {
            string sql = string.Format(@"{0} WHERE Enabled=1 AND WebsiteSolutionId=?WebsiteSolutionId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteSolutionId", websiteSolutionId, DbType.Int32);
            return await this.databaseContext.Slave.QueryAsync<WebsiteSolutionItemEntity>(sql, parameters);
        }
        #endregion

        #region 获取官网解决方案项目分页
        public async Task<(IEnumerable<WebsiteSolutionItemEntity>, int)> Query(string solutionName, string solutionItemName, bool? enabled, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (!string.IsNullOrWhiteSpace(solutionName))
            {
                wheres.Add("SolutionName LIKE CONCAT('%',CONCAT(?SolutionName,'%'))");
                parameters.Add("?SolutionName", solutionName?.Trim(), DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(solutionItemName))
            {
                wheres.Add("SolutionItemName LIKE CONCAT('%',CONCAT(?SolutionItemName,'%'))");
                parameters.Add("?SolutionItemName", solutionItemName?.Trim(), DbType.AnsiString);
            }
            if (enabled.HasValue)
            {
                wheres.Add("Enabled=?Enabled");
                parameters.Add("?Enabled", enabled.Value, DbType.Boolean);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM websitesolutionitem {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize,
                  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.Slave.QueryMultipleAsync<WebsiteSolutionItemEntity, int>(sql, parameters);
        }
        #endregion

        #region 启用官网解决方案项目
        public async Task<bool> Enabled(int websiteSolutionItemId)
        {
            string sql = @"UPDATE websitesolutionitem SET Enabled=1 WHERE WebsiteSolutionItemId=?WebsiteSolutionItemId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteSolutionItemId", websiteSolutionItemId, DbType.Int32);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 禁用官网解决方案项目
        public async Task<bool> Disabled(int websiteSolutionItemId)
        {
            string sql = @"UPDATE websitesolutionitem SET Enabled=0 WHERE WebsiteSolutionItemId=?WebsiteSolutionItemId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteSolutionItemId", websiteSolutionItemId, DbType.Int32);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion
    }
}



